﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ESF.General;
using System.Diagnostics;
using System.Net;
using System.Deployment.Application;
using System.ServiceModel;
using ESF.General.Text;
using System.ComponentModel;
using System.Threading;
using ESF.WinForms;
using ESF.SFT.FileUploader.SharedResources;
using ESF.SFT.FileUploader.Presenter.Users;
using ESF.WinForms.Elements;
using ESF.General.Entity;

#endregion


namespace ESF.SFT.FileUploader
{
	/// <summary>
	/// Interaction logic for SignInWindow.xaml
	/// </summary>
	public partial class SignInWindow : BaseWPFWindow, ISignInWindowViewer
	{
		#region Miembros

		private BackgroundWorker _Bg;

		private SignInWindowPresenter _Presenter;

		#endregion


		#region Constructor

		public SignInWindow()
			: base()
		{
			InitializeComponent();

			imgUsers.Source = WinFormsHelper.ToBitmapSource(SharedImages.User_64x64);

			Icon = WinFormsHelper.ToBitmapSource(SharedImages.Upload_64x64);

			txtUserName.Focus ();

			
			_Presenter = new SignInWindowPresenter(this);

			_Presenter.Load ();

			IncludeVersionInTitle ();
			

			_Bg = new BackgroundWorker();

			_Bg.DoWork += delegate(object s, DoWorkEventArgs eg) { _Presenter.SignIn (); };
			_Bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_Bg_RunWorkerCompleted);
		}

		#endregion


		#region Funciones

		public void InitializeWindows ()
		{
			Dispatcher.Invoke ((Action) (() => 
			{ 
				if (Program.winFilUp == null)
				{
					Program.winFilUp = new FileUploaderWindow ();
				}
			} ));
		}


		public void FocusUserName()
		{
			Focus (txtUserName);
		}


		public void FocusUserPassword()
		{
			Focus (txtUserPassword);
		}

		#endregion


		#region Button Events

		private void btnSignin_Click(object sender, RoutedEventArgs e)
		{
			btnSignin.IsEnabled = false;

			_Bg.RunWorkerAsync();
		}

		#endregion


		#region Label Events

		private void lnkSignUp_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			ProcessHelper.StartProcess(Program.Conf.First(c => c.Key == "SignUpURL").Value);
		}

		private void lnkForgetPassword_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			ProcessHelper.StartProcess(Program.Conf.First(c => c.Key == "ForgetPasswordURL").Value);
		}

		#endregion


		#region TextBlock Events

		private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
		{
			frmInfoMsg = Texts.CondicionesServicio;
		}

		#endregion


		#region Window Events

		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			CloseApplication ();
		}

		#endregion


		#region StatusBar Events

		private void stsStatusBar_DoubleClick(object sender, MouseButtonEventArgs pEvent)
		{
			frmInfoMsg = Log.GetOperationTimeStatistics();
		}

		#endregion


		#region Background Worker Events

		protected void _Bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			FaultException gEx;


			btnSignin.IsEnabled = true;

			if (e.Error != null)
			{
				LogError(e.Error);

				if (e.Error is FaultException)
				{
					gEx = e.Error as FaultException;

					if (gEx.Code.Name.Contains (Program.Conf.Find(c => c.Key == "ExpiredPassword_Code").Value)
						|| gEx.Code.Name.Contains (Program.Conf.Find(c => c.Key == "ChangePassword_Code").Value))
					{
						ProcessHelper.StartProcess(Program.Conf.Find(c => c.Key == "ChangePasswordURL").Value);
					}
				}

				return;
			}
			
			Log.StopOps();

			Program.winFilUp.LoadConfiguration ();
			Program.winFilUp.Show();
			
			Hide();
		}

		#endregion


		#region ISignInWindowViewer Fields

		public string UserName
		{
			get { return GetText (txtUserName); }
		}

		public string UserPassword
		{
			get { return GetPassword (txtUserPassword); }
		}

		public string ProductVersion
		{
			set { stsStatusBar.ToolTip = string.Format("Versión: {0}", value); }
		}

		public List<FileRegisterValidatorSet> FileRegisterValidatorSets { set { Program.FileRegisterValidatorSets = value; } }

		#endregion
	}
}
